import wx
from wx import adv

from config import Version
from serve import func
from utils.constants import LOGO_PATH


class TaskBarIcon(adv.TaskBarIcon):
    """开启任务托盘"""

    def __init__(self, frame):
        adv.TaskBarIcon.__init__(self)
        self.frame = frame
        self.SetIcon(wx.Icon(name=LOGO_PATH, type=wx.BITMAP_TYPE_ICO), Version().app_name)
        self.Bind(wx.adv.EVT_TASKBAR_LEFT_DOWN, self.show_window)

    def show_window(self, event):
        if self.frame.IsIconized():      # 判断窗口是否是系统托盘
            self.frame.Iconize(False)    # 恢复窗口
        if not self.frame.IsShown():     # 判断窗口是否隐藏
            self.frame.Show(True)        # 显示窗口
        self.frame.Raise()               # 将窗口提升到顶层

    def exit(self, event):
        func.force_exit()

    def CreatePopupMenu(self):
        menu = wx.Menu()
        menu.Append(1, '显示')
        self.Bind(wx.EVT_MENU, self.show_window, id=1)
        menu.Append(2, '退出')
        self.Bind(wx.EVT_MENU, self.exit, id=2)
        return menu
